<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>客户来源类型维护</title>
</head>
<body>
<div id="app">
    <input type="text" v-model="addValue"  placeholder = "输入客户来源类型" />
    <Button v-on:click="save" >保存</Button>
    <hr>
    <i-table :columns="sourceTypeListHead" :data="sourceTypeList"  ref="table"></i-table>
</div>



<script src="../../vendors/require.js"></script>
<script>
    require.config({
        paths: {
            'jquery': '../../vendors/jquery-3.2.1.min',
            'vue': '../../vendors/vue/vue.min',
            'iview': '../../vendors/iview/js/iview',
            'api': '../../api/api-config',
            'CryptoJS':'../../vendors/crypto-js',
        }
    });

    require(['jquery','vue',  'iview','api'],
        function ($,Vue, iview,api) {
            Vue.use(iview);
            var vm = new Vue({
                el: "#app",
                data: {
                    sourceTypeListHead: [
                        {
                            title: '客户来源类型',
                            width: 120,
                            key: 'SourceType',
                        },
                        {
                            title: '删除',
                            width: 80,
                            key: 'operation',
                            render: (function (params) {
                                return (
                                    ' <a v-on:click="operation(' + params._index + ')">删除</a>'
                                )
                            })
                        }
                    ],
                    sourceTypeList:[],
                    addValue:"",
                },
                mounted: function () {
                    this.searchSourceType();
                },
                methods: {
                    save: function () {
                        if($.trim(this.addValue)=="")
                        {
                            this.$Message.error('不能为空');
                            return ;
                        }
                        var thisSourceType = this;
                        api.Post_Add_Customer_SourceType({SourceType: thisSourceType.addValue})
                            .done(function (data) {
                                if (data.IsSuccess) {
                                    thisSourceType.searchSourceType();
                                } else {
                                    thisSourceType.$Message.error(data.ErrMsg);
                                }
                            })
                            .fail(function () {
                                thisSourceType.$Message.error('保存失败');
                            })
                    },
                    operation: function (index) {
                        if(confirm("确定要删除")) {
                            var thisSourceType = this;
                            api.Post_Del_Customer_SourceType({SourceType: thisSourceType.sourceTypeList[index].SourceType})
                                .done(function (data) {
                                    if (data.IsSuccess) {
                                        thisSourceType.searchSourceType();
                                    } else {
                                        thisSourceType.$Message.error(data.ErrMsg);
                                    }
                                })
                                .fail(function () {
                                    thisSourceType.$Message.error('删除失败');
                                })
                        }
                    },
                    searchSourceType:function () {
                        var thisSourceType=this;
                        api.Get_Customer_SourceType_Option({ companyId: localStorage.companyId })
                            .done(function (data) {
                                if (data.IsSuccess) {
                                    thisSourceType.sourceTypeList =[];
                                    for(var i=0;i<data.ResultObject.length;i++)
                                    {
                                        thisSourceType.sourceTypeList.push({SourceType:data.ResultObject[i]});
                                    }
                                } else {
                                    thisSourceType.$Message.error(data.ErrMsg);
                                }
                            })
                            .fail(function () {
                                thisSourceType.$Message.error('查询出错');
                            })
                    },


                }
            });
        });


</script>
</body>
</html>
